﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AuthorManage.aspx.cs" Inherits="CooliteExamples.Web.Pages.Control.AuthorManage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
		<title></title>
		<ext:ScriptContainer runat="server" />
</head>
<body>
		<ext:ScriptManager runat="server" />

		<script language="javascript" type="text/javascript">
				var btnAdd_Click = function(sender, args) {
						PopulateModal(0, "");
						ModifyAuthorWindow.setTitle("Add Author");
						ModifyAuthorWindow.show();
				}

				var gpAuthor_Command = function(cmd, args) {
						var record = args.data;
						if (cmd == "Edit") {
								PopulateModal(record.AuthorId, record.AuthorName);
								ModifyAuthorWindow.setTitle("Modify Author");
								ModifyAuthorWindow.show();
						}
						else {
								Ext.MessageBox.confirm("Confirm Delete", "Are you sure you wish to delete this author?", function(result) {
										if (result == "yes") {
												Coolite.AjaxMethods.DeleteAuthor(record.AuthorId, {
														success: function(result) {
																AuthorStore.load();
														},
														failure: function(msg) {
																Ext.MessageBox.show({
																		title: "An Error Occured",
																		msg: msg,
																		buttons: Ext.MessageBox.OK,
																		icon: Ext.MessageBox.ERROR
																});
														},
														eventMask: {
																showMask: true,
																msg: "Deleting Author..."
														}
												});
										}
								});
						}
				}

				function PopulateModal(authorId, authorName) {
						txtAuthorName.setValue(authorName);
						hidAuthorId.setValue(authorId);
				}
		</script>

		<ext:Store ID="AuthorStore" runat="server" AutoLoad="true" AutoDataBind="false">
				<Reader>
						<ext:JsonReader Root="data">
								<Fields>
										<ext:RecordField Name="AuthorId" />
										<ext:RecordField Name="AuthorName" />
								</Fields>
						</ext:JsonReader>
				</Reader>
		</ext:Store>
		<form id="form1" runat="server">
		<ext:Panel ID="pnlMain" runat="server" Height="420" Border="false" StyleSpec="margin: 6px;"
				ButtonAlign="Right">
				<Body>
						<ext:GridPanel ID="gpAuthor" runat="server" StripeRows="true" StoreID="AuthorStore"
								Height="400" Width="400" Title="Manage Authors">
								<ColumnModel runat="server">
										<Columns>
												<ext:Column Header="ID" DataIndex="AuthorId" Width="50" />
												<ext:Column Header="Name" DataIndex="AuthorName" />
												<ext:CommandColumn Width="40">
														<Commands>
																<ext:GridCommand CommandName="Edit" Icon="ApplicationEdit" />
																<ext:CommandSeparator />
																<ext:GridCommand CommandName="Delete" Icon="Delete" />
														</Commands>
												</ext:CommandColumn>
										</Columns>
								</ColumnModel>
								<View>
										<ext:GridView runat="server" ForceFit="true" />
								</View>
								<Listeners>
										<Command Fn="gpAuthor_Command" />
								</Listeners>
								<Buttons>
										<ext:Button ID="btnAdd" runat="server" Icon="Add" Text="Add Author">
												<Listeners>
														<Click Fn="btnAdd_Click" />
												</Listeners>
										</ext:Button>
								</Buttons>
						</ext:GridPanel>
				</Body>
		</ext:Panel>
		<!-- Modify Window -->
		<ext:Window ID="ModifyAuthorWindow" runat="server" AutoHeight="true" ButtonAlign="Right"
				CenterOnLoad="true" Icon="ApplicationEdit" Modal="true" ShowOnLoad="false" Title="Add Author"
				Width="500">
				<Body>
						<ext:FormPanel ID="fpAuthor" runat="server" AutoHeight="true" BodyStyle="padding: 4px;"
								Method="POST">
								<Body>
										<ext:FormLayout runat="server" LabelWidth="100" LabelAlign="Left">
												<ext:Anchor>
														<ext:TextField ID="txtAuthorName" runat="server" Width="200" FieldLabel="Author Name" />
												</ext:Anchor>
												<ext:Anchor>
														<ext:Hidden ID="hidAuthorId" runat="server" Text="0" />
												</ext:Anchor>
										</ext:FormLayout>
								</Body>
						</ext:FormPanel>
				</Body>
				<Buttons>
						<ext:Button ID="btnCancel" runat="server" Icon="Cancel" Text="Cancel">
								<Listeners>
										<Click Handler="ModifyAuthorWindow.hide();" />
								</Listeners>
						</ext:Button>
						<ext:Button ID="btnSave" runat="server" Icon="Accept" Text="Save">
								<AjaxEvents>
										<Click OnEvent="btnSave_Click" Type="Submit" Method="POST" />
								</AjaxEvents>
						</ext:Button>
				</Buttons>
		</ext:Window>
		</form>
</body>
</html>
